package com.glaze.service.Impl;

import com.glaze.dao.entity.Account;
import com.glaze.dao.mapper.AccountMapper;
import com.glaze.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @Description:
 * @auther ZhouLiGuo
 * @Date 2024/4/22 10:50 PM:54
 * @Version 1.0
 * @Copyright: 琉璃出品，必属精品
 */

@Service
public class TradeServiceImpl implements TradeService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void transfer() {
       transfer("030023450001","030023450002",new BigDecimal("10"));
    }


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void transfer(String fromAccount, String toAccount, BigDecimal amt){
        transferAdd(toAccount,amt);
        int b = 1/0;
        transferSub(fromAccount,amt);
    }

    private void transferAdd(String to, BigDecimal amt){
        Account onAccount = new Account();
        onAccount.setAccount(to);
        onAccount.setBalAmt(amt);
        accountMapper.increaseAmt(onAccount);
    }



    private void transferSub(String fromAccount, BigDecimal amt){
        Account offAccount = new Account();
        offAccount.setAccount(fromAccount);
        offAccount.setBalAmt(amt);
        accountMapper.decreaseAmt(offAccount);
    }
}
